Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for non-exhaustive patterns #31020

Merged
merged 4 commits into from
Jan 27, 2016
Merged

Improve error message for non-exhaustive patterns #31020

merged 4 commits into from
Jan 27, 2016

Conversation

regexident
Copy link
Contributor

Changes error message from displaying first found missing constructor witness to showing up to 10, if necessary.

Fixes issue #16884.

Changes error message from displaying first found missing constructor witness to showing up to 10, if necessary.

Fixes issue #16884.
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

span_err!(cx.tcx.sess, sp, E0004,
"non-exhaustive patterns: `{}` not covered",
pat_to_string(witness)
pattern_strings.join("`, `")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe delimit the last element with "and"? I think this reads a bit weird otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Was wondering about this, too.

Would something like this look good to you?

let joined_string = match pattern_strings {
    [pattern] => pattern,
    [head.., tail] => head.join("`, `") + "` and `" + tail
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also uses slice patterns, and apparently requires the "advanced_slice_pattern" feature gate.

I'm not sure about the optimal solution, maybe something like this?

let joined_string = match pattern_strings.split_last() {
    Some((tail, head)) => {
        if head.is_empty() {
            tail.clone()
        } else {
            head.join("`, `") + "` and `" + tail
        }
    },
    None => unreachable!()
};

More human-readable error message showing ellipsis for excessively long witness lists.
@brson
Copy link
Contributor

brson commented Jan 19, 2016

I'm guessing you are making this change because it's annoying to fix the error just to be given a nearly identical message. I think this is a fine reason.

I might suggest that 10 is too many though, based on the rule of thumb that people can generally keep 7 +/3 'things' straight at a time. @nikomatsakis do we have any precedent for how many 'things' to display before cutting errors off?

It may be worth saying "... and more (not shown)" or something for the case where we do cut them off. @nikomatsakis do we have existing precedent for what language to use for that?

@regexident
Copy link
Contributor Author

Hi brson, thanks for the comment!

I totally agree, that 10 is probably quite a bit too much. I guess even three would be suffice in the end. After all if you're missing that many patterns in the first place, then you might want to consult the subject's type definition anyway, in order to proceed (instead of like copy/paste the missing patterns from the error message). As such, I suppose, even a limit as low as 3 should work here. After all it should do, is make it clear that there's more than one missing case, when there is.

I quickly searched the project for "and more" idioms but couldn't find anything that'd be of a similar enough scenario to imitate here. The phrases "and more", "not shown" as well as "..." (as a list ellipsis) appear to actually not even exist within src/test/….

Maybe @steveklabnik knows more about such a precedent or has suggestions for a proper fit from the overall point of view of the docs?

Further more I used an actual Unicode ellipsis glyph (), but should be using three periods (...) instead, I suppose, as that's what's used everywhere else. Makes sense to stay within ASCII for mostly terminal/console use, I guess.

@nikomatsakis
Copy link
Contributor

Hmm, I was trying to think of good precedent, but I'm coming up blank. I think there are other places we've added semi-arbitrary cutoffs, but I'm not sure where that is right now. In particular, I was looking at the suggestions for traits to import in method dispatch, but those seem to print all applicable options (those are in librustc_typeck/check/method/suggest.rs).

Still, I agree that just printing 2 or 3 is plenty. If we know the full set, you could print something like "...and 7 more possibilities." Otherwise, it feels like we ought to be able to just insert some wording to make it clear that these are just representative, and there are more possibilities than the ones we've printed.

@regexident
Copy link
Contributor Author

I adjusted the error message to now print in either of these formats:

  1. Print … non-exhaustive patterns: Red not covered. (1)
  2. Print … non-exhaustive patterns: East, SouthandWest not covered. (1...3)
  3. Print … non-exhaustive patterns: Second, Third, Fourth and 8 more not covered. (4…)

depending on the number of missing patterns.

@regexident
Copy link
Contributor Author

@brson, @nikomatsakis: Any other changes you'd like me to make to this?
Or put differently: It's my first PR on /rust-lang/rust/, what's the next steps from here?

@nikomatsakis
Copy link
Contributor

@regexident sounds awesome! :)

let used_constructors: Vec<Constructor> = rows.iter()
.flat_map(|row| pat_constructors(cx, row[0], left_ty, max_slice_length))
.collect();
all_constructors(cx, left_ty, max_slice_length)
.into_iter()
.find(|c| !used_constructors.contains(c))
.filter(|c| !used_constructors.contains(c)).collect()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you put the .collect() on the next line, to match the formatting above?

@nikomatsakis
Copy link
Contributor

This looks good to me modulo the nits above. @regexident can you update with those small nits addressed (and then leave a comment, since otherwise I don't get any notifications)?

@regexident
Copy link
Contributor Author

Sure, I'm at it already. Thanks for the comments!

@regexident
Copy link
Contributor Author

@nikomatsakis:
All done.
👍

@brson
Copy link
Contributor

brson commented Jan 27, 2016

@bors r+ Thanks @regexident

@bors
Copy link
Contributor

bors commented Jan 27, 2016

📌 Commit 2c7a19a has been approved by brson

@bors
Copy link
Contributor

bors commented Jan 27, 2016

⌛ Testing commit 2c7a19a with merge aba11b3...

bors added a commit that referenced this pull request Jan 27, 2016
Changes error message from displaying first found missing constructor witness to showing up to 10, if necessary.

Fixes issue #16884.
@bors bors merged commit 2c7a19a into rust-lang:master Jan 27, 2016
@regexident regexident deleted the fix_16884 branch January 27, 2016 10:35
@regexident
Copy link
Contributor Author

@brson@nikomatsakis

🙇

@brson brson added the relnotes Marks issues that should be documented in the release notes of the next release. label Jan 29, 2016
arielb1 added a commit to arielb1/rust that referenced this pull request Mar 4, 2016
bors added a commit that referenced this pull request Mar 6, 2016
alexcrichton pushed a commit to alexcrichton/rust that referenced this pull request Mar 16, 2016
@frewsxcv
Copy link
Member

An issue was just opened relevant to this: #36410

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants